package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.SysRoleService;
import com.atguigu.spzx.model.dto.system.SysRoleDto;
import com.atguigu.spzx.model.dto.system.SysUserDto;
import com.atguigu.spzx.model.entity.system.SysRole;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.h5.UserInfoVo;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/system/sysRole")
@Tag(name = "角色管理")
@CrossOrigin(allowCredentials = "true" , originPatterns = "*" , allowedHeaders = "*")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    @Operation(summary = "分页查询角色")
    @GetMapping("/findByPage/{pageNum}/{pageSize}")
    public Result<PageInfo<SysRole>>  findByPage(@RequestParam("roleName") String rolename,
                                                 @PathVariable(value = "pageNum") Integer pageNum ,
                                                 @PathVariable(value = "pageSize") Integer pageSize){
        PageInfo<SysRole> sysRolePageInfo=sysRoleService.findByPage(rolename,pageNum,pageSize);
        return Result.build(sysRolePageInfo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "添加角色数据")
    @PostMapping("/saveSysRole")
    public Result saveSysRole(@RequestBody SysRole sysRole){
        sysRoleService.saveSysRole(sysRole);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @PutMapping("/updateSysRole")
    @Operation(summary = "修改角色数据")
    public Result updateSysRole(@RequestBody SysRole sysRole){
        sysRoleService.updateSysRole(sysRole);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @DeleteMapping("/deleteById/{roleId}")
    @Operation(summary = "删除角色数据")
    public Result removeById(@PathVariable(value = "roleId") Integer id){
        sysRoleService.removeById(id);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/findAllRoles/{userId}")
    @Operation(summary = "查询所有角色和当前用户对应的角色")
    public Result<Map<String , Object>> findAll(@PathVariable("userId") Long userId){

        Map<String , Object> map=sysRoleService.findAll(userId);
        return Result.ok(map);
    }
}
